package net.webservicex.stockcurrencyconvert;

import javax.jws.WebMethod;
import javax.jws.WebService;

import net.webservicex.currency.Currency;
import net.webservicex.currency.CurrencyConvertor;
import net.webservicex.currency.CurrencyConvertorSoap;
import net.webservicex.stock.StockQuote;
import net.webservicex.stock.StockQuoteSoap;

@WebService (endpointInterface = "net.webservicex.stockcurrencyconvert.StockPriceConvert")
public class StockPriceConvertImp implements StockPriceConvert {
	private final String CLOSE = "<Last>|</Last>";
	private final String OPEN = "<Open>|</Open>";
	private final String HIGH = "<High>|</High>";
	private final String LOW = "<Low>|</Low>";
	private final String RANGE = "<AnnRange>|</AnnRange>";
	private final String NAME = "<Name>|</Name>";
	private final String CHANGE = "<Change>|</Change>";
	

	@WebMethod
	public String convertStockClose(String symbol, String currency) {
		Double d = convert(getQuote(symbol,CLOSE),currency);
		String s = String.format((getName(symbol) + " closed at %.2f " + currency), d);
		return s;
	}


	@WebMethod
	public String convertStockOpen(String symbol, String currency) {
		Double d = convert(getQuote(symbol,OPEN),currency);
		String s = String.format((getName(symbol) + " opened at %.2f " + currency), d);
		return s;
	}


	@WebMethod
	public String convertStockHigh(String symbol, String currency) {
		Double d = convert(getQuote(symbol,HIGH),currency);
		String s = String.format((getName(symbol) + " had a %.2f " + currency +" high"), d);
		return s;
	}


	@WebMethod
	public String convertStockLow(String symbol, String currency) {
		Double d = convert(getQuote(symbol,LOW),currency);
		String s = String.format((getName(symbol) + " had a %.2f " + currency +" low"), d);
		return s;
	}


	@WebMethod
	public String convertAnnualRange(String symbol, String currency) {
		String s = getName(symbol) + " has an annual range of "+(convertRange(symbol, currency)) + " "+ currency ;
		return s;
	}
	
	@WebMethod
	public String convertStockChange(String symbol, String currency){
		Double d = convert(getQuote(symbol,CHANGE),currency);
		String s = String.format((getName(symbol) + " had a change of %.2f " + currency), d);
		return s;
	}
	
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~PRIVATE METHODS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	
	/*
	 * STOCK METHODS
	 */
	private Double getQuote(String symbol, String regex){
		 StockQuote service = new StockQuote();
	     StockQuoteSoap port = service.getStockQuoteSoap();
	     String s = port.getQuote(symbol);
	     String [] t = s.split(regex);
	     String result = t[1];
	     return Double.parseDouble(result);
	}
	@WebMethod
	public String getName(String symbol){
		StockQuote service = new StockQuote();
	     StockQuoteSoap port = service.getStockQuoteSoap();
	     String s = port.getQuote(symbol);
	     String [] t = s.split(NAME);
	     return t[1];
	}
	
	private String getRange(String symbol){
		 StockQuote service = new StockQuote();
	     StockQuoteSoap port = service.getStockQuoteSoap();
	     String s = port.getQuote(symbol);
	     String [] t = s.split(RANGE);
	     return t[1];
	}
	
	/*
	 * CONVERSION METHODS
	 */
	
	
	//convert based on currency
	private Double convert(Double value, String currency){
		 Double rate=1.0;
		 if(!currency.equals("USD")){
		 CurrencyConvertor converter = new CurrencyConvertor();
		 CurrencyConvertorSoap port = converter.getCurrencyConvertorSoap();
		 rate = port.conversionRate(Currency.fromValue("USD"), Currency.fromValue(currency));
		 }
		 return value * rate;
	}

	private String convertRange(String symbol, String currency){
		 double temp;
		 String range;
	     range = getRange(symbol);
	     String [] t= range.split(" - ");
	     temp = Double.parseDouble(t[0]);
	     range = String.format("%.2f",convert(temp, currency));
	     range += " - ";
	     temp = Double.parseDouble(t[1]);
	     range += String.format("%.2f", convert(temp, currency));
	     return range;
	     
	}
	

	public static void main(String args[]){
		StockPriceConvertImp stock = new StockPriceConvertImp();
		System.out.println(stock.convertAnnualRange("AAPL", "USD"));
	}

}
